package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {
	@Autowired
	private BaseTrademarkService baseTrademarkService;
	
	/**
	 * 根据param查询品牌列表
	 * @param category3Id
	 * @return
	 */
	@Override
	public List<BaseTrademark> baseCategorTrademark(Long category3Id) {
		LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
		List<BaseCategoryTrademark> baseCategoryTrademarkList = this.list(queryWrapper);
		
		if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
			List<Long> trademarkId = baseCategoryTrademarkList.stream().map((baseCategoryTrademark) -> {
				return baseCategoryTrademark.getTrademarkId();
			}).collect(Collectors.toList());
			
			List<BaseTrademark> list = baseTrademarkService.listByIds(trademarkId);
			return list;
		}
		
		return null;
	}
	/**
	 * 查询未关联的品牌列表
	 * @param category3Id
	 * @return
	 */
	
	@Override
	public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
		BaseTrademarkMapper baseMapper = (BaseTrademarkMapper) baseTrademarkService.getBaseMapper();
		List<BaseTrademark> baseTrademarks= baseMapper.getCurrentTrademarkList(category3Id);
		return baseTrademarks;
		
	}
	
	/**
	 * 关联品牌列表
	 * @param categoryTrademarkVo
	 * @return
	 */
	@Override
	public void saveBase(CategoryTrademarkVo categoryTrademarkVo) {
	List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
	if (!CollectionUtils.isEmpty(trademarkIdList)){
		List<BaseCategoryTrademark> collect = trademarkIdList.stream().map((trademarkId) -> {
			BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
			baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
			baseCategoryTrademark.setTrademarkId(trademarkId);
			return baseCategoryTrademark;
		}).collect(Collectors.toList());
		this.saveBatch(collect);
	}
	}
	/**
	 * 删除关联品牌
	 * @param category3Id
	 * @param trademarkId
	 * @return
	 */
	@Override
	public void deleteBaseCate(Long category3Id, Long trademarkId) {
		LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
		queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
		this.remove(queryWrapper);
	}
}
